package com.chainyoung.component_tmperson.ui.activity.cloudwallet;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.appcommon.hdwallet.OwnWalletUtils;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.view.ActionSheetDialog;
import com.chainyoung.common.view.FixedCursorEditText;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.CoinItem;
import com.chainyoung.component_tmperson.entity.PurseAddrItem;
import com.chainyoung.component_tmperson.ui.activity.ScanQrCodeActivity;
import com.chainyoung.component_tmperson.ui.mvp.contract.AddPurseContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.AddPursePresenter;
import com.chainyoung.component_tmperson.zxing.Result;
import com.zxing.activity.ScanActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.chainyoung.common.appcommon.hdwallet.CoinConstant.ETH;
import static com.chainyoung.component_tmperson.ui.fragment.MyPurseAddrListFragment.TYPE_CLOUD;
import static com.chainyoung.component_tmperson.ui.fragment.MyPurseAddrListFragment.TYPE_LOCAl;

public class AddMyPurseActivity extends BaseActivity<AddPursePresenter> implements AddPurseContract.View {


    @BindView(R2.id.txt_select_coin)
    TextView txtSelectCoin;
    @BindView(R2.id.tv_select_coin)
    TextView tvSelectCoin;
    @BindView(R2.id.txt_purse_name)
    TextView txtPurseName;
    @BindView(R2.id.et_purse_name)
    FixedCursorEditText etPurseName;
    @BindView(R2.id.txt_purse_addr)
    TextView txtPurseAddr;
    @BindView(R2.id.et_purse_addr)
    FixedCursorEditText etPurseAddr;
    @BindView(R2.id.tv_submit)
    TextView tvSubmit;

    private String coinName;

    private List<CoinItem> mCoinList = new ArrayList<>();
    private List<String> coinNames = new ArrayList<>();
    private PurseAddrItem item;
    private String addr;
    private String type;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        item = (PurseAddrItem) getIntent().getSerializableExtra("purseItem");
        type = getIntent().getStringExtra("type");

        if (item != null) {
            initView();
        }
        setCurrentTitle(getString(R.string.tmperson_add_addr));
        Drawable drawable=ContextCompat.getDrawable(mContext,R.mipmap.ic_scan);
        DrawableCompat.setTint(drawable, Color.parseColor("#061c69"));
        showAndSetBarRightIcon(drawable);


    }

    private void initView() {

    }

    private final static int REQUEST_CODE = 0x111;

    @Override
    public void onRightImgClicked(View v) {
        super.onRightImgClicked(v);
        launchForResult(ScanQrCodeActivity.class, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

        String result = data.getStringExtra("data");

        List<String> wordsList = new ArrayList<>();
        try {
            wordsList.addAll(Arrays.asList(result.split(" ")));
        } catch (Exception ignore) {

        }
        if (wordsList.size() == 12) {
            addr = OwnWalletUtils.getWalletAddr(result, ETH);
        } else if (result.contains(":")) {
            addr = result.substring(result.lastIndexOf(":") + 1);
        }

        etPurseAddr.setText(addr);
        etPurseAddr.setSelection(addr.length());

    }

    @Override
    protected void initData() {
        getCoinList();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_add_purse_addr;
    }

    @OnClick(R2.id.tv_select_coin)
    void selectCoin() {
        new ActionSheetDialog(mContext).builder().setCancelable(true).setCanceledOnTouchOutside(true)
                .addSheetItems(coinNames, listener)
                .show();

//                .addSheetItem("BIT", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() {
//                    @Override
//                    public void onClick(int which) {
//                        coinName = "BIT";
//                        tvSelectCoin.setText(coinName);
//                    }
//                }).
//                addSheetItem("BTC", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() {
//                    @Override
//                    public void onClick(int which) {
//                        coinName = "BTC";
//                        tvSelectCoin.setText(coinName);
//                    }
//                })
    }

    @OnClick(R2.id.tv_submit)
    void submit() {
        if (StringUtil.viewGetTextIsEmpty(tvSelectCoin)) {
            ToastUtil.showToast(tvSelectCoin.getHint().toString());
            return;
        }
        if (StringUtil.viewGetTextIsEmpty(etPurseName)) {
            ToastUtil.showToast(etPurseName.getmHint().toString());
            return;
        }
        if (StringUtil.viewGetTextIsEmpty(etPurseAddr)) {
            ToastUtil.showToast(etPurseAddr.getmHint().toString());
            return;
        }

        if (StringUtil.containsEmoji(etPurseName.getText().toString().trim()) ||
                StringUtil.isHaveCNSymbol(etPurseName.getText().toString().trim())) {
            ToastUtil.showToast("钱包名称不能包含表情符号和中文符号");
            return;
        }


        if (type.equals(TYPE_CLOUD)) {
            if (!StringUtil.isWalletAddrValidate(etPurseAddr.getText().toString().trim())) {
                ToastUtil.showToast("请输入正确的钱包地址");
                return;
            }
        } else if (type.equals(TYPE_LOCAl)) {
            if (!StringUtil.isETHWalletAddrValidate(etPurseAddr.getText().toString().trim())) {
                ToastUtil.showToast("请输入正确的钱包地址");
                return;
            }
        }


        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("coin", coinName.toLowerCase());
        map.put("wallet_name", StringUtil.viewGetText(etPurseName));
        map.put("address", StringUtil.viewGetText(etPurseAddr));
        if (item != null) {
            map.put("address_id", item.getId());
        }
        map.put("wallet_type", type);
        mPresenter.addPurseAddr(map);


    }

    ActionSheetDialog.OnSheetItemClickListener listener = new ActionSheetDialog.OnSheetItemClickListener() {
        @Override
        public void onClick(int which) {
            coinName = coinNames.get(which - 1);
            tvSelectCoin.setText(coinName);
        }
    };


    //----------------------------------------------------------网络请求-----------------------------------------

    private void getCoinList() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("wallet_type", type);
        mPresenter.getCoinList(map);
    }

    @Override
    public void onGetCoinListSuccess(List<CoinItem> data) {
        if (data == null) {
            return;
        }
        mCoinList.clear();
        mCoinList.addAll(data);
        coinNames.clear();
        for (CoinItem coin : mCoinList) {
            coinNames.add(coin.getCoin().toUpperCase());
        }
    }

    @Override
    public void onAddPurseAddrSuccess(String data) {
        ToastUtil.showToast(data);
        finish();
    }


}
